<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.wan888888.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Model\BannerModel;
use Admin\Model\CategoryModel;
use Common\Model\TreeModel;
/**
 * 活动管理控制器
 * @author liuyang
 */
class BannerController extends AdminController {

 /**
     * 配置管理
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        /* 查询条件初始化 */
        $map = array();
        $map  = array('isshow' => 1); 
        if(isset($_GET['name'])){
            $map['name']    =   array('like', '%'.(string)I('name').'%');
        }

        $list = $this->lists('act_banner', $map,'id DESC,orderno');
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);

        $this->assign('group',C('CONFIG_GROUP_LIST'));
        //$this->assign('group_id',I('get.group',0));
        $this->assign('list', $list);
        $this->meta_title = '广告管理';
        $this->display();
    }

   

    /* 编辑活动 */
    public function edit($id = null, $category_id = 0){
        $Banner = (new BannerModel());

        if(IS_POST){ //提交表单 
            if($_REQUEST['url_type']==1){
                $_POST['url']=$_REQUEST['act_url'];
            }
            if(false !== $Banner->update()){
                $this->success('编辑成功！', U('index'));
            } else {
                $error = $Banner->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = '';
            if($category_id){
                /* 获取上级活动信息 */
                $cate = (new CategoryModel())->info($category_id, '*');
                if(!($cate && 1 == $cate['isshow'])){
                    $this->error('指定的上级活动不存在或被禁用！');
                }
            }

            /* 获取活动信息 */
            $info = $id ? $Banner->info($id) : '';
            if($info['url_type']==1){
                
                $info['act_url']=$info['url'];
                $info['url']="";
            }
            $catelist = (new CategoryModel())->getlist();
            $newcatelist = (new TreeModel())->toFormatTree($catelist,"name","id","parentid");
            $this->assign('catelist', $newcatelist);
            $this->assign('info',       $info);
            $this->assign('category',   $cate);
            $this->meta_title = '编辑活动';
            $this->display();
        }
    }

    /* 新增活动 */
    public function add($category_id = 0){
        $Banner = (new BannerModel());

        if(IS_POST){ //提交表单
            if($_REQUEST['url_type']==1){
                $_POST['url']=$_REQUEST['act_url'];
            }
            if(false !== $Banner->update()){
                
                $this->success('新增成功！', U('index'));
            } else {
                $error = $Banner->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = array();
            if($category_id){
                /* 获取上级活动信息 */
                $cate = (new CategoryModel())->info($category_id, '*');
                if(!($cate && 1 == $cate['isshow'])){
                    $this->error('指定的上级活动不存在或被禁用！');
                }
            }
            $catelist = (new CategoryModel())->getlist();
            $newcatelist = (new TreeModel())->toFormatTree($catelist,"name","id","parentid"); 
            /* 获取活动信息 */
            $this->assign('info',       null);
            $this->assign('category', $cate);
            $this->assign("catelist",$newcatelist);
            $this->meta_title = '新增活动';
            $this->display('edit');
        }
    }

    /**
     * 删除一个活动
     * @author huajie <banhuajie@163.com>
     */
    public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }
 

        //删除该活动信息
        $res = (new BannerModel())->delete($cate_id);
        if($res !== false){
            //记录行为
            action_log('update_banner', 'banner', $cate_id, UID);
            $this->success('删除活动成功！');
        }else{
            $this->error('删除活动失败！');
        }
    }
 

   
    
}
